{{- if and .Values.global.modules.publicDomainTemplate .Values.global.clusterIsBootstrapped }}
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: documentation
  namespace: d8-system
  {{- include "helm_lib_module_labels" (list . (dict "app" "documentation")) | nindent 2 }}
  annotations:
    web.deckhouse.io/export-name: "documentation"
    web.deckhouse.io/export-icon: "/public/img/docs.png"
  {{- if and (ne (include "helm_lib_module_https_mode" .) "Disabled") .Values.documentation.auth.externalAuthentication }}
    nginx.ingress.kubernetes.io/auth-signin: {{ .Values.documentation.auth.externalAuthentication.authSignInURL | quote }}
    nginx.ingress.kubernetes.io/auth-url: {{ .Values.documentation.auth.externalAuthentication.authURL | quote }}
    nginx.ingress.kubernetes.io/configuration-snippet: |
  {{- else }}
    nginx.ingress.kubernetes.io/auth-type: basic
    nginx.ingress.kubernetes.io/auth-secret: documentation-basic-auth
    nginx.ingress.kubernetes.io/auth-realm: "Authentication Required"
    nginx.ingress.kubernetes.io/configuration-snippet: |
      proxy_ssl_certificate /etc/nginx/ssl/client.crt;
      proxy_ssl_certificate_key /etc/nginx/ssl/client.key;
      proxy_ssl_protocols TLSv1.2;
      proxy_ssl_session_reuse on;
  {{- end }}
      rewrite ^/$ /en/platform/ permanent;
      rewrite ^/(en|ru)/$ /$1/platform/ permanent;
      rewrite ^/modules/(.+) /en/modules/$1 permanent;
      rewrite ^/platform/(.+) /en/platform/$1 permanent;
      rewrite ^/(ru|en)/(modules/[0-9]+-[^/]+/.*)$ /$1/platform/$2 permanent;
      rewrite ^/(en|ru)/modules/810-deckhouse-web(/.*)$ /$1/modules/810-documentation$2 permanent;
      rewrite ^/documentation/v[\d]+[^\/]*/(.*)?$ /en/platform/$3 permanent;
      rewrite ^(/(ru|en))/documentation/v[\d]+[^\/]*/(.*)?$ $1/platform/$3 permanent;
  {{- if .Values.documentation.auth.satisfyAny }}
    nginx.ingress.kubernetes.io/satisfy: "any"
  {{- end }}
spec:
  ingressClassName: {{ include "helm_lib_module_ingress_class" . | quote }}
  rules:
  - host: {{ include "helm_lib_module_public_domain" (list . "documentation") }}
    http:
      paths:
      - backend:
          service:
            name: documentation
            port:
              name: http
        path: /
        pathType: ImplementationSpecific
      - backend:
          service:
            name: documentation
            port:
              name: http
        path: /modules
        pathType: Prefix
  {{- if (include "helm_lib_module_https_ingress_tls_enabled" .) }}
  tls:
    - hosts:
        - {{ include "helm_lib_module_public_domain" (list . "documentation") }}
      secretName: {{ include "helm_lib_module_https_secret_name" (list . "ingress-tls") }}
  {{- end }}
{{- if eq (include "helm_lib_module_https_mode" .) "CertManager" }}
---
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: documentation
  namespace: d8-system
  {{- include "helm_lib_module_labels" (list . (dict "app" "documentation")) | nindent 2 }}
spec:
  certificateOwnerRef: false
  secretName: {{ include "helm_lib_module_https_secret_name" (list . "ingress-tls") }}
  {{ include "helm_lib_module_generate_common_name" (list . "documentation") | nindent 2 }}
  dnsNames:
  - {{ include "helm_lib_module_public_domain" (list . "documentation") }}
  issuerRef:
    name: {{ include "helm_lib_module_https_cert_manager_cluster_issuer_name" . }}
    kind: ClusterIssuer
{{- end }}
---
{{- end }}
